/**    
 * @{#} BuweiActivity.java Create on 2014-6-9 下午8:50:36    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import cn.o.app.event.listener.OnSelectedChangeListener;
import cn.o.app.ui.OTab;
import cn.skinapp.R;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.evan.common.pullrefresh.PullToRefreshBase;
import com.evan.common.pullrefresh.PullToRefreshBase.OnRefreshListener;
import com.evan.common.pullrefresh.PullToRefreshListView;
import com.smiier.skin.adapter.QuestionAdapter;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;

public class DoctorAnswerActivity extends BasicActivity implements
		OnRefreshListener<ListView>, OnItemClickListener {

	private Button current_btn;

	private PullToRefreshListView list_jiedazhong, list_yiwancheng;

	private QuestionAdapter jiedazhongAdapter, yiwanchengAdapter;

	private final int TYPE_JIEDAZHONG = 0, TYPE_YIWANCHENG = 1;

	private int type = TYPE_JIEDAZHONG;

	int start_qid, start_qid_jiedazhong, start_qid_yiwancheng;

	HashMap<String, Object> params = new HashMap<String, Object>();

	PullToRefreshListView currentList;
	ArrayList<Object> array_jiedazhong = new ArrayList<Object>(),
			array_yiwancheng = new ArrayList<Object>();

	private LinearLayout ll_btns;

	TextView text_empty;
	private OTab mTab;
	boolean isFinishAnswer = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_doctor_my_answer);

		init();
		setNavTitle("我的解答");

		list_jiedazhong = (PullToRefreshListView) findViewById(R.id.list_question_jiedazhong);
		list_jiedazhong.setOnRefreshListener(this);
		list_jiedazhong.setScrollLoadEnabled(true);
		list_jiedazhong.setPullRefreshEnabled(false);
		list_jiedazhong.setPullLoadEnabled(false);
		list_jiedazhong.setOnItemClickListener(this);

		list_yiwancheng = (PullToRefreshListView) findViewById(R.id.list_question_yiwancheng);
		list_yiwancheng.setOnRefreshListener(this);
		list_yiwancheng.setScrollLoadEnabled(true);
		list_yiwancheng.setPullRefreshEnabled(false);
		list_yiwancheng.setPullLoadEnabled(false);
		list_yiwancheng.setOnItemClickListener(this);

		ll_btns = (LinearLayout) findViewById(R.id.ll_btns);
		mTab = findViewById(R.id.tab, OTab.class);
		mTab.setOnSelectedChangeListener(mListener);
		mTab.setSelectedIndex(0);
		text_empty = (TextView) findViewById(R.id.text_empty);
		text_empty.setText("您还未接单");
		currentList = list_jiedazhong;

		isFinishAnswer = getIntent().getBooleanExtra(Constant.IDENTITY_KEY_1,
				false);
		params.put(Constant.PARAM_API, "Question.GetList");
		params.put("pageSize", Constant.pageSize);
		params.put("is_user_del_question", -1);
		if (isFinishAnswer) {
			mTab.setVisibility(View.GONE);
			try {
				JSONObject doctorInfo = new JSONObject(getIntent()
						.getStringExtra(Constant.IDENTITY_KEY));
				params.put("doctor_uid", doctorInfo.getString("Uid"));
				setNavTitle(doctorInfo.getString("Name") + "的解答");
				ll_btns.setVisibility(View.GONE);
			} catch (Exception e) {
				e.printStackTrace();
			}
			loadData(false);
		} else {
			params.put("doctor_uid", GlobalSettings.sUid);
		}
		
		refreshView();
	}
	
	
	private void refreshView() {
		Intent noInfo = new Intent();
		noInfo.setAction(Constant.RECIVER_MYIFNO_TAB_NOREAD);
		noInfo.putExtra("show", false);

	   this.sendBroadcast(noInfo);
	   
	   Intent noItem = new Intent();
	   noItem.putExtra("show", false);
	   noItem.setAction(Constant.RECIVER_MYIFNO_ITEM_NOREAD);
	   this.sendBroadcast(noItem);
		
	}

	private OnSelectedChangeListener mListener = new OnSelectedChangeListener() {

		@Override
		public void onChanged(View v, int index) {
			if (index == 0) {
				if (!CommonUtility.isNull(currentList)) {
					currentList.setVisibility(View.GONE);
				}
				currentList = list_jiedazhong;
				currentList.setVisibility(View.VISIBLE);
				type = TYPE_JIEDAZHONG;
				text_empty.setText("您还未接单");
			} else if (index == 1) {
				if (!CommonUtility.isNull(currentList)) {
					currentList.setVisibility(View.GONE);
				}
				currentList = list_yiwancheng;
				currentList.setVisibility(View.VISIBLE);
				type = TYPE_YIWANCHENG;
				text_empty.setText("您还未有完成的问题");
			}

			if (type == TYPE_JIEDAZHONG
					&& CommonUtility.isNull(jiedazhongAdapter)) {
				loadData(false);
			} else if (type == TYPE_YIWANCHENG
					&& CommonUtility.isNull(yiwanchengAdapter)) {
				loadData(true);
			} else {
				if (type == TYPE_JIEDAZHONG) {
					if (!CommonUtility.isNull(array_jiedazhong)
							&& array_jiedazhong.size() > 0) {
						text_empty.setVisibility(View.GONE);
					} else {
						text_empty.setVisibility(View.VISIBLE);
					}
				} else if (type == TYPE_YIWANCHENG) {
					if (!CommonUtility.isNull(array_yiwancheng)
							&& array_yiwancheng.size() > 0) {
						text_empty.setVisibility(View.GONE);
					} else {
						text_empty.setVisibility(View.VISIBLE);
					}
				}
			}

		}
	};

	protected void onResume() {
		super.onResume();
		loadData(false);
	};

	/**
	 * method desc：
	 */
	public void loadData(boolean isShowDialog) {
		if (isFinishAnswer) {

		} else {
			if (type == TYPE_JIEDAZHONG) {
				start_qid = start_qid_jiedazhong;
				params.put("statuses", 2);
			} else {
				params.put("statuses", 3);
				start_qid = start_qid_yiwancheng;
			}
		}
		params.put("startQid", start_qid);
		RequestTaskIntercept.requestIntercept(activity, Constant.URL.REQUEST,
				new RequestHandler(activity, new HandlerCallback() {

					@Override
					public void callback(Object o) {
						list_jiedazhong.doComplete();
						list_yiwancheng.doComplete();
						if (!CommonUtility.isNull(o)) {
							try {
								JSONObject object = (JSONObject) o;
								if (CommonUtility.response200(object)) {
									object = object.getJSONObject("Res");
									if (type == TYPE_JIEDAZHONG) {
										JSONArray arr = (JSONArray) object
												.getJSONArray("Questions");
										if (arr.length() > 0) {
											CommonUtility
													.putAll(array_jiedazhong,
															arr, true);
											currentList.setHasMoreData(true);
										} else {
											if (array_jiedazhong.size() > 0) {
												currentList
														.setHasMoreData(false);
											}
										}

										if (array_jiedazhong.size() > 0) {
											start_qid_jiedazhong = ((JSONObject) array_jiedazhong
													.get(array_jiedazhong
															.size() - 1))
													.getInt("Qid");
											text_empty.setVisibility(View.GONE);
										} else {
											text_empty
													.setVisibility(View.VISIBLE);
										}

										if (CommonUtility
												.isNull(jiedazhongAdapter)) {
											jiedazhongAdapter = new QuestionAdapter(
													activity, array_jiedazhong);
											currentList
													.setAdapter(jiedazhongAdapter);
										} else {
											jiedazhongAdapter.notifyUpdate();
										}
									} else if (type == TYPE_YIWANCHENG) {
										JSONArray arr = (JSONArray) object
												.getJSONArray("Questions");
										if (arr.length() > 0) {
											CommonUtility
													.putAll(array_yiwancheng,
															arr, true);
											currentList.setHasMoreData(true);
										} else {
											if (array_yiwancheng.size() > 0) {
												currentList
														.setHasMoreData(false);
											}
										}

										if (array_yiwancheng.size() > 0) {
											start_qid_yiwancheng = ((JSONObject) array_yiwancheng
													.get(array_yiwancheng
															.size() - 1))
													.getInt("Qid");
											text_empty.setVisibility(View.GONE);
										} else {
											text_empty
													.setVisibility(View.VISIBLE);
										}

										if (CommonUtility
												.isNull(yiwanchengAdapter)) {
											yiwanchengAdapter = new QuestionAdapter(
													activity, array_yiwancheng);
											currentList
													.setAdapter(yiwanchengAdapter);
										} else {
											yiwanchengAdapter.notifyUpdate();
										}
									}
								} else {
									CommonUtility.toast(activity,
											R.string.server_unavailable);
								}
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}
				}), isShowDialog, params);
	}

	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
	}

	@Override
	public void onPullUpToLoadMore(PullToRefreshBase<ListView> refreshView) {
		loadData(false);
	}

	@Override
	public void onItemClick(AdapterView<?> adapterView, View view, int arg2,
			long arg3) {
		JSONObject object = (JSONObject) view.getTag(R.id.tag_obj);
		Intent intent = new Intent(activity, QuestionDetailActivity.class);
		intent.putExtra(Constant.IDENTITY_KEY, object.toString());
		startActivity(intent);
	}

	/*
	 * @Override public void onResume() { super.onResume();
	 * MobclickAgent.onResume(getContext()); }
	 * 
	 * @Override public void onPause() { super.onPause();
	 * MobclickAgent.onPause(getContext()); }
	 */

}
